local qiaoduan = fk.CreateSkill{
  name = "rfenghou_3d14j__qiaoduan",
}

qiaoduan:addEffect(fk.ChainStateChanged, {
  anim_type = "offensive",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(qiaoduan.name) then
      if target.chained then
        local x = #table.filter(player.room:getAllPlayers(), function(p) return p.chained end)
        return player:getMark("rfenghou_3d14j__qiaoduan1-turn") == 0 and #player:getCardIds("he") >= x
      elseif not target.chained then
        return player:getMark("rfenghou_3d14j__qiaoduan2-turn") == 0
      end
    end
  end,
  on_cost = function (self, event, target, player, data)
    local room = player.room
    local x = #table.filter(room:getAllPlayers(), function(p) return p.chained end)
      if target.chained then
        room:setPlayerMark(player, "rfenghou_3d14j__qiaoduan1-turn", 1)
        local to = room:askToChoosePlayers(player, {
          targets = table.filter(room:getAllPlayers(), function(p)
          return p:isWounded() end),
          min_num = 1,
          max_num = 1,
          prompt = "#rfenghou_3d14j__qiaoduan-choose1:::"..x,
          skill_name = qiaoduan.name,
          cancelable = true
        })
        if #to > 0 then
          event:setCostData(self, to[1])
          return true
        end
      elseif not target.chained then
       room:setPlayerMark(player, "rfenghou_3d14j__qiaoduan2-turn", 1)
      local to = room:askToChoosePlayers(player, {
        targets = room:getAllPlayers(),
        min_num = 1,
        max_num = x,
        prompt = "#rfenghou_3d14j__qiaoduan-choose2:::"..x,
        skill_name = qiaoduan.name,
        cancelable = true
      })
        if #to > 0 then
          event:setCostData(self, to)
          return true
        end
      end
    end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    if target.chained then
      local x = #table.filter(room:getAllPlayers(), function(p) return p.chained end)
      local cards = room:askToCards(player, {
        min_num = x,
        max_num = x,
        include_equip = true,
        skill_name = qiaoduan.name,
        pattern = ".",
        prompt = "#rfenghou_3d14j__qiaoduan-card:::"..x
      })
      if #cards > 0 then
        if #cards > 1 then
          cards = room:askToGuanxing(player, {
            cards = cards,
            top_limit = { 0, 0 },
            bottom_limit = { #cards, #cards },
            skill_name = qiaoduan.name,
            skip = true
          }).bottom
          cards = table.reverse(cards)
        end
        room:moveCards({
          ids = cards,
          from = player.id,
          toArea = Card.DrawPile,
          moveReason = fk.ReasonJustMove,
          skillName = qiaoduan.name,
          proposer = player,
          drawPilePosition = -1,
        })
      end
      local to = event:getCostData(self)
      if not to.dead and to:isWounded() then
        room:recover({
          who = to,
          num = 1,
          recoverBy = player,
          skillName = qiaoduan.name
        })
      end
    elseif not target.chained then
      local tos = event:getCostData(self)
      room:sortByAction(tos)
      for _, to in ipairs(tos) do
        if not to.dead then
          room:drawCards(to, 1, qiaoduan.name)
        end
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["rfenghou_3d14j__qiaoduan"] = "巧锻",
  [":rfenghou_3d14j__qiaoduan"] = "每回合首次有角色的武将牌被：横置后，你可以将X张牌置于牌堆底，令一名角色回复1点体力；重置后，你可以令至多X名角色各摸一张牌(X为处于连环状态的角色数)。",

  ["#rfenghou_3d14j__qiaoduan-choose1"] = "巧锻：是否令一名角色回复1点体力（选择后，你须先将 %arg 张牌置于牌堆底）",
  ["#rfenghou_3d14j__qiaoduan-choose2"] = "巧锻：是否令至多 %arg 名角色各摸一张牌？",
  ["#rfenghou_3d14j__qiaoduan-card"] = "巧锻：请将 %arg 张牌置于牌堆底！",
}

return 
